package com.huterox.linuxrember.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.huterox.linuxrember.Dao.AdditionDBEngine;
import com.huterox.linuxrember.Dao.CommandDBEngine;
import com.huterox.linuxrember.Dao.ReviewDBEngine;
import com.huterox.linuxrember.Dao.UserDBEngine;
import com.huterox.linuxrember.R;

public class GrammarActivity extends AppCompatActivity {


    private TextView tv_grammar;
    private Button bt_break;

    private CommandDBEngine commandDBEngine;
    private UserDBEngine userDBEngine;
    private AdditionDBEngine additionDBEngine;
    private ReviewDBEngine reviewDBEngine;

    public void StudyAction(View view, int id, int flag) {
        Intent intent = new Intent(this, StudyActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        bundle.putInt("flag", flag);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    public void ReviewAction(View view, int id) {
        Intent intent = new Intent(this, ReviewActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        intent.putExtras(bundle);
        startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grammar);

        userDBEngine = new UserDBEngine(this);
        commandDBEngine = new CommandDBEngine(this);
        additionDBEngine = new AdditionDBEngine(this);
        reviewDBEngine = new ReviewDBEngine(this);

        initView();

    }
    private void initView() {

        tv_grammar = findViewById(R.id.tv_grammar);
        bt_break = findViewById(R.id.bt_break);

        GrammarActivity.MyOnClickListener l = new GrammarActivity.MyOnClickListener();
        tv_grammar.setOnClickListener(l);
        bt_break.setOnClickListener(l);
        Intent intent = getIntent();
        int id = intent.getExtras().getInt("id");
        int flag = intent.getExtras().getInt("flag");
        String k = intent.getExtras().getString("command");

        if (flag == 1) {
            String tb_name = userDBEngine.query_tbnameByid(getWindow().getDecorView(), id);
            int now = userDBEngine.querynowByid(getWindow().getDecorView(), id, tb_name);
            commandDBEngine.open();
            int total = commandDBEngine.queryTotal(getWindow().getDecorView(), tb_name);
            if (now > total) {
                now = total;
            }
            commandDBEngine.open();
            String a = commandDBEngine.queryGrammarByNum(getWindow().getDecorView(), tb_name, now);
            if (a.contains("\\n")) {

                String b = a.replace("\\n", "\n");
                tv_grammar.setText(b);
            } else {
                tv_grammar.setText(a);
            }
        } else if (flag == 2) {
            int now = userDBEngine.querynowByid(getWindow().getDecorView(), id, "chapter8");
            int t = additionDBEngine.queryTotal(getWindow().getDecorView());
            if (now > t) {
                now = t;
            }
            String a = additionDBEngine.queryGrammar(getWindow().getDecorView(), now);
            if (a.contains("\\n")) {

                String b = a.replace("\\n", "\n");
                tv_grammar.setText(b);
            } else {
                tv_grammar.setText(a);
            }
        } else if (flag == 3) {
            reviewDBEngine.open();
            String a = reviewDBEngine.queryGrammarBycommand(getWindow().getDecorView(), k);
            if (a.contains("\\n")) {
                String b = a.replace("\\n", "\n");
                tv_grammar.setText(b);
            } else {
                tv_grammar.setText(a);
            }
        }
    }
    private class MyOnClickListener implements View.OnClickListener {

        Intent intent = getIntent();
        int id = intent.getExtras().getInt("id");//获取传递来的id
        int flag = intent.getExtras().getInt("flag");
        @Override
        public void onClick(View view) {
            switch (view.getId()) {

                case R.id.bt_break:
                    if (flag == 1 || flag == 2) {

                        StudyAction(view, id, flag);
                    }
                    if (flag == 3) {

                        ReviewAction(view, id);
                    }

                    break;
            }
        }
    }
}